#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''host instance factory'''

import sys
import traceback
from modules import *

hosts = {}

def getHost(hostname, user='', pwd=''):
    _hostname = hostname.lower()
    
    #get existed instance
    host = None
    if hosts.get(_hostname, None) is not None:
        return hosts[_hostname]
    
    #check account
    if user == '' or pwd == '':
        print >>sys.stderr, 'It\'s the first time to connect to', _hostname
        print >>sys.stderr, 'please input user and password'
        return None
    
    #generate host instance
    try:
        exec('host=%s.%s(user,pwd)' % (_hostname, _hostname))
    except NameError:
        print >>sys.stderr, 'Unsupported Host:', _hostname
        return None
        
    hosts[_hostname] = host
    
    return host
        
if __name__ == '__main__':
    #test
    hostInstance = getHost('hoile')
    hostInstance = getHost('hotfile')
    if hostInstance is not None:
        hostInstance.login()
    hostInstance = getHost('hotfile','airnessub','19841012')
    if hostInstance is not None:
        hostInstance.login()
    